package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getUserAddressBook(){
        log.info("查看当前用户所有的地址");
        List<AddressBook> userAddressBook = addressBookService.getUserAddressBook();
        return Result.success(userAddressBook);
    }

    @PutMapping("/default")
    public Result defaultAddress(@RequestBody AddressBook addressBook){
        log.info("设置地址为默认地址：{}",addressBook.getId());
        addressBookService.updateDefaultAddress(addressBook.getId());
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(Long id){
        log.info("修改地址根据id回显数据：{}",id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    public Result deleteById(Long id){
        log.info("根据id删除地址:{}",id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/default")
    public Result getDefaultAddress(){
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefaultAddress();
        return Result.success(addressBook);
    }

}
